<template>
  <div class="private-chat-container">
    <!-- 顶部导航栏 -->
    <div class="chat-header">
      <el-button icon="el-icon-arrow-left" @click="goBack" circle></el-button>
      <div class="user-info">
        <img :src="currentUser.avatar" :alt="currentUser.name" class="user-avatar" />
        <div class="user-details">
          <div class="user-name">{{ currentUser.name }}</div>
          <div class="user-status">{{ currentUser.online ? '在线' : '离线' }}</div>
        </div>
      </div>
      <div class="chat-actions">
        <el-button icon="el-icon-phone" circle></el-button>
        <el-button icon="el-icon-video-camera" circle></el-button>
        <el-dropdown @command="handleMoreAction">
          <el-button icon="el-icon-more" circle></el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="clear">清空聊天记录</el-dropdown-item>
              <el-dropdown-item command="mute">消息免打扰</el-dropdown-item>
              <el-dropdown-item command="complaint">投诉</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>
    
    <!-- 消息显示区域 -->
    <div class="chat-messages" ref="messagesContainer">
      <div class="message-date">2024年1月15日</div>
      
      <!-- 对方消息 -->
      <div class="message-item received">
        <img :src="currentUser.avatar" :alt="currentUser.name" class="message-avatar" />
        <div class="message-content">
          <div class="message-text">你好！今天过得怎么样？</div>
          <div class="message-time">14:30</div>
        </div>
      </div>
      
      <!-- 自己的消息 -->
      <div class="message-item sent">
        <div class="message-content">
          <div class="message-text">还不错，准备去爬山呢！</div>
          <div class="message-time">14:32</div>
        </div>
      </div>
      
      <!-- 对方消息 -->
      <div class="message-item received">
        <img :src="currentUser.avatar" :alt="currentUser.name" class="message-avatar" />
        <div class="message-content">
          <div class="message-text">
            <div class="message-text-content">昨天的会议记录在这里，请查收</div>
            <div class="message-file">
              <i class="el-icon-document"></i>
              <span class="file-name">会议记录.xlsx</span>
              <el-button type="text" @click="downloadFile">下载</el-button>
            </div>
          </div>
          <div class="message-time">14:35</div>
        </div>
      </div>
      
      <!-- 自己的消息 -->
      <div class="message-item sent">
        <div class="message-content">
          <div class="message-text">收到，谢谢分享！</div>
          <div class="message-time">14:37</div>
        </div>
      </div>
      
      <!-- 对方消息 -->
      <div class="message-item received">
        <img :src="currentUser.avatar" :alt="currentUser.name" class="message-avatar" />
        <div class="message-content">
          <div class="message-text">😊 不客气，有问题随时问我</div>
          <div class="message-time">14:40</div>
        </div>
      </div>
    </div>
    
    <!-- 底部输入区域 -->
    <div class="chat-input">
      <div class="input-actions">
        <el-button icon="el-icon-picture" circle @click="selectImage"></el-button>
        <el-button icon="el-icon-folder" circle @click="selectFile"></el-button>
        <el-button icon="el-icon-microphone" circle @click="recordVoice"></el-button>
        <el-dropdown @command="handleExtraAction">
          <el-button icon="el-icon-more" circle></el-button>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="emoji">表情</el-dropdown-item>
              <el-dropdown-item command="location">位置</el-dropdown-item>
              <el-dropdown-item command="contact">名片</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
      <div class="input-container">
        <el-input
          type="textarea"
          v-model="messageContent"
          placeholder="请输入消息..."
          :rows="3"
          @keydown.ctrl.enter="addNewLine"
          @keydown.enter.exact.prevent="sendMessage"
        ></el-input>
      </div>
      <div class="send-actions">
        <div class="send-tip">按Enter发送，Ctrl+Enter换行</div>
        <el-button type="primary" @click="sendMessage">发送</el-button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "PrivateChat",
  data() {
    return {
      currentUser: {
        id: 1,
        name: "张三",
        avatar: require('@/assets/default-avatar.png'),
        online: true
      },
      messageContent: ""
    };
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    
    handleMoreAction(command) {
      switch (command) {
        case 'clear':
          this.$confirm('确定要清空聊天记录吗？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.$message.success('聊天记录已清空');
          });
          break;
        case 'mute':
          this.$message.info('消息免打扰功能待实现');
          break;
        case 'complaint':
          this.$message.info('投诉功能待实现');
          break;
      }
    },
    
    handleExtraAction(command) {
      switch (command) {
        case 'emoji':
          this.$message.info('表情功能待实现');
          break;
        case 'location':
          this.$message.info('位置功能待实现');
          break;
        case 'contact':
          this.$message.info('名片功能待实现');
          break;
      }
    },
    
    downloadFile() {
      this.$message.success('文件下载功能待实现');
    },
    
    selectImage() {
      this.$message.info('选择图片功能待实现');
    },
    
    selectFile() {
      this.$message.info('选择文件功能待实现');
    },
    
    recordVoice() {
      this.$message.info('语音录制功能待实现');
    },
    
    addNewLine() {
      this.messageContent += '\n';
    },
    
    sendMessage() {
      if (!this.messageContent.trim()) {
        this.$message.warning('请输入消息内容');
        return;
      }
      
      // 模拟发送消息
      this.$message.success('消息发送成功');
      this.messageContent = "";
      
      // 滚动到底部
      this.$nextTick(() => {
        const container = this.$refs.messagesContainer;
        container.scrollTop = container.scrollHeight;
      });
    }
  },
  mounted() {
    // 组件挂载后滚动到底部
    this.$nextTick(() => {
      const container = this.$refs.messagesContainer;
      container.scrollTop = container.scrollHeight;
    });
  }
};
</script>

<style scoped>
.private-chat-container {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  min-height: 100vh;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  background: rgba(255, 255, 255, 0.2);
  border-bottom: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  border-radius: 15px;
  margin: 20px;
}

.user-info {
  display: flex;
  align-items: center;
}

.user-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 10px;
  object-fit: cover;
  border: 2px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.user-details {
  display: flex;
  flex-direction: column;
}

.user-name {
  font-size: 16px;
  font-weight: 500;
  color: #333;
}

.user-status {
  font-size: 12px;
  color: #999;
}

.chat-actions {
  display: flex;
  gap: 10px;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 15px;
  margin: 0 20px 20px 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

.message-date {
  text-align: center;
  font-size: 12px;
  color: #999;
  margin-bottom: 20px;
}

.message-item {
  display: flex;
  margin-bottom: 20px;
  max-width: 80%;
}

.message-item.received {
  align-self: flex-start;
}

.message-item.sent {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.message-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  margin: 0 10px;
  object-fit: cover;
}

.message-content {
  display: flex;
  flex-direction: column;
}

.message-item.received .message-content {
  align-items: flex-start;
}

.message-item.sent .message-content {
  align-items: flex-end;
}

.message-text {
  padding: 10px 15px;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.5;
  max-width: 100%;
  word-wrap: break-word;
}

.message-item.received .message-text {
  background: rgba(255, 255, 255, 0.3);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-top-left-radius: 0;
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.message-item.sent .message-text {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  border-top-right-radius: 0;
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.message-text-content {
  margin-bottom: 5px;
}

.message-file {
  display: flex;
  align-items: center;
  padding: 8px;
  background-color: #f5f5f5;
  border-radius: 4px;
  font-size: 12px;
}

.message-file .el-icon-document {
  margin-right: 5px;
  color: #1890ff;
}

.file-name {
  flex: 1;
  margin-right: 10px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.message-time {
  font-size: 12px;
  color: #999;
  margin-top: 5px;
}

.message-item.received .message-time {
  align-self: flex-start;
}

.message-item.sent .message-time {
  align-self: flex-end;
}

.chat-input {
  background: rgba(255, 255, 255, 0.2);
  border-top: 1px solid rgba(255, 255, 255, 0.2);
  padding: 15px 20px;
  border-radius: 15px;
  margin: 0 20px 20px 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

.input-actions {
  display: flex;
  gap: 10px;
  margin-bottom: 10px;
}

.input-container {
  margin-bottom: 10px;
}

.send-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.send-tip {
  font-size: 12px;
  color: #999;
}
</style>